package com.seafwg.ssm.service.impl;

import com.seafwg.ssm.domain.Account;
import com.seafwg.ssm.mapper.AccountMapper;
import com.seafwg.ssm.service.AccountService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author seafwg
 * @Date 2022/3/18 9:11 上午
 * @Version 1.0
 * @description xxx
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    // 传统写法
    public List<Account> findAll() {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();

        List<Account> accountList = sqlSession.getMapper(AccountMapper.class).findAll();

        return accountList;
    }

    // SqlSessionFactory工厂的创建权交由spring管理
    public List<Account> findAll1() {
        List<Account> accountList = accountMapper.findAll();
        return accountList;
    }


    public Account findAccountById(int id) {
        Account account = accountMapper.findAccountById(id);
        return account;
    }

}
